import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from hyrobot.common import CHECK_POINT, STEP

#逻辑判断一下是否左栏隐藏
# .is-active > div:nth-child(1) > svg:nth-child(1)

def login(driver,usrn,psw):
    driver.find_element(By.NAME, "username").send_keys(usrn)
    driver.find_element(By.NAME, "password").send_keys(psw)
    driver.find_element(By.CSS_SELECTOR, ".el-button").click()

def logout(driver):
    time.sleep(2)
    driver.find_element(By.CSS_SELECTOR,
    ".avatar-wrapper > span:nth-child(1)").click()
    time.sleep(3)
    driver.find_element(By.CSS_SELECTOR,
    "li.el-dropdown-menu__item:nth-child(2) > span:nth-child(1)").click()


class c73302:
    name = "验证登陆后显示id，权限功能-73302"
    driver = webdriver.Firefox()

    def teststeps(self):
        self.driver.implicitly_wait(3)
        self.driver.get("http://172.17.66.19:8080/")

        STEP(1, '输入普通用户的账号和正确的密码，点击登录')
        username = 'user4'
        psw = '123456'
        login(self.driver,username,psw)
        # self.driver.find_element(By.NAME, "username").send_keys(username)
        # self.driver.find_element(By.NAME, "password").send_keys('123456')
        # self.driver.find_element(By.CSS_SELECTOR, ".el-button").click()

        got_username = self.driver.find_element(By.CSS_SELECTOR,
        ".avatar-wrapper > span:nth-child(1)").text
        CHECK_POINT("id是否正确",username == got_username)
        # hide = self.driver.find_element(By.CSS_SELECTOR,".is-active > div:nth-child(1) > svg:nth-child(1)")
        got_list0 = []
        extending_button = self.driver.find_element_by_id("hamburger-container")
        try:
            self.driver.find_element(By.CLASS_NAME, "el-tooltip")
            extending_button.click()
        except:
            print("左侧栏没有隐藏")
        product_word0 = self.driver.find_element(By.XPATH,
        "/html/body/div/div/div[1]/ul/div[1]/a/li/span").text
        got_list0.append(product_word0)
        apply_word0 = self.driver.find_element(By.CSS_SELECTOR, ".el-menu > div:nth-child(2) > a:nth-child(1) > li:nth-child(1) > span:nth-child(2)").text
        got_list0.append(apply_word0)
        expetced_word_list0 = ["产品列表","申请记录"]
        CHECK_POINT("验证是否为期待文字列表", expetced_word_list0 == got_list0)

# logout
        logout(self.driver)

        STEP(2,'输入产品管理员的账号和正确的密码，点击登录')
        username1 = 'zhangsan'
        psw1 = '123456'
        login(self.driver,username1,psw1)

        got_username1 = self.driver.find_element(By.CSS_SELECTOR,
                            ".avatar-wrapper > span:nth-child(1)").text
        CHECK_POINT("id是否正确", username1 == got_username1)
        got_list1 = []
        product_word1 = self.driver.find_element(By.XPATH,
        "/html/body/div/div/div[1]/ul/div[1]/a/li/span").text
        got_list1.append(product_word1)
        apply_word1 = self.driver.find_element(By.CSS_SELECTOR,
        ".el-menu > div:nth-child(2) > a:nth-child(1) > li:nth-child(1) > span:nth-child(2)").text
        got_list1.append(apply_word1)
        product_manage_word1 = self.driver.find_element(By.CSS_SELECTOR,
        ".el-menu > div:nth-child(3) > a:nth-child(1) > li:nth-child(1) > span:nth-child(2)").text
        got_list1.append(product_manage_word1)

        expetced_word_list1 = ["产品列表", "申请记录","产品管理"]
        CHECK_POINT("验证是否为期待文字列表", expetced_word_list1 == got_list1)
        logout(self.driver)

        STEP(3, '输入超级管理员的账号和正确的密码，点击登录')
        username2 = 'sunxidong'
        psw2 = '123456'
        login(self.driver, username2, psw2)

        got_username2 = self.driver.find_element(By.CSS_SELECTOR,
                 ".avatar-wrapper > span:nth-child(1)").text
        CHECK_POINT("id是否正确", username2 == got_username2)
        got_list2 = []
        product_word2 = self.driver.find_element(By.XPATH,
            "/html/body/div/div/div[1]/ul/div[1]/a/li/span").text
        got_list2.append(product_word2)
        apply_word2 = self.driver.find_element(By.CSS_SELECTOR,
        ".el-menu > div:nth-child(2) > a:nth-child(1) > li:nth-child(1) > span:nth-child(2)").text
        got_list2.append(apply_word2)
        product_manage_word2 = self.driver.find_element(By.CSS_SELECTOR,
        ".el-menu > div:nth-child(3) > a:nth-child(1) > li:nth-child(1) > span:nth-child(2)").text
        got_list2.append(product_manage_word2)
        user_manage_word2 = self.driver.find_element(By.CSS_SELECTOR,
        ".el-menu > div:nth-child(4) > a:nth-child(1) > li:nth-child(1) > span:nth-child(2)").text
        got_list2.append(user_manage_word2)

        expetced_word_list2 = ["产品列表", "申请记录", "产品管理","用户管理"]
        CHECK_POINT("验证是否为期待文字列表", expetced_word_list2 == got_list2)
        logout(self.driver)

        self.driver.quit()